// hotel-management-frontend/src/services/api.js

import axios from 'axios';

const API_BASE_URL = 'http://localhost:5000/api'; // IMPORTANT: Confirm your backend URL

// Admin API client
const adminAxios = axios.create({
    baseURL: API_BASE_URL + '/admin',
});

// Client API client
const clientAxios = axios.create({
    baseURL: API_BASE_URL + '/client',
});

export const adminAPI = {
    // Room Management
    addRoom: (roomData) => adminAxios.post('/rooms', roomData),
    getAllRooms: (params) => adminAxios.get('/rooms', { params }),
    getAvailableRooms: () => adminAxios.get('/rooms/available'), // Used for check-in dropdown
    updateRoomStatus: (roomId, status) => adminAxios.put(`/rooms/${roomId}/status`, { status }),

    // !!! 关键修改: 修正 bulkImportRooms 的路径 !!!
    bulkImportRooms: (data) => adminAxios.post('/rooms/import', data),

    // Guest Management
    addGuest: (guestData) => adminAxios.post('/guests', guestData),
    searchGuests: (params) => adminAxios.get('/guests', { params }),
    updateGuest: (id, guestData) => adminAxios.put(`/guests/${id}`, guestData),
    deleteGuest: (id) => adminAxios.delete(`/guests/${id}`),
    getAllGuests: () => adminAxios.get('/guests'),

    // Check-in Management
    createCheckin: (checkinData) => adminAxios.post('/checkins', checkinData),
    getAllCheckins: () => adminAxios.get('/checkins'),
    getActiveCheckins: () => adminAxios.get('/checkins/active'),
    getCheckinById: (id) => adminAxios.get(`/checkins/${id}`),
    updateCheckin: (id, checkinData) => adminAxios.put(`/checkins/${id}`, checkinData),
    checkoutGuest: (checkinId, checkoutData) => adminAxios.post(`/checkins/${checkinId}/checkout`, checkoutData),

    // Consumption Management (Admin can add/view all)
    addConsumption: (consumptionData) => adminAxios.post('/consumptions', consumptionData),
    getConsumptionsByCheckin: (checkinId) => adminAxios.get(`/consumptions/${checkinId}`), // Admin views consumption for a specific checkin

    // Statistics & Dashboard (Admin only)
    getDashboardOverview: () => adminAxios.get('/dashboard/overview'),
    getRoomStats: (params) => adminAxios.get('/stats/rooms', { params }),
    getConsumptionStats: (params) => adminAxios.get('/stats/consumptions', { params }),

    // Menu Item Management (Admin only) - Assuming you still have a menu items table/concept
    // If 'MenuItems' table is deleted, remove these or comment out.
    getAllMenuItems: () => adminAxios.get('/menuitems'),
    addMenuItem: (itemData) => adminAxios.post('/menuitems', itemData),
    // 这个 bulkImportMenuItems 看起来路径是正确的，因为它会解析为 /api/admin/menuitems/import
    bulkImportMenuItems: (formData) => adminAxios.post('/menuitems/import', formData, {
        headers: { 'Content-Type': 'multipart/form-data' }
    }),
};


export const clientAPI = {
    // Client API calls for a specific guest based on ID card
    getGuestByCard: (idCard) => clientAxios.get(`/guests/${idCard}`), // Check backend route: /client/guests/:idCard
    getCheckinsByGuestCard: (idCard) => clientAxios.get(`/checkins/${idCard}`), // Check backend route: /client/checkins/:idCard
    addClientConsumption: (checkinId, consumptionData) => clientAxios.post(`/checkins/${checkinId}/consumptions`, consumptionData),
    getClientConsumption: (checkinId) => clientAxios.get(`/checkins/${checkinId}/consumptions`), // Client views their own checkin's consumption
    getClientRoomDetails: (roomId) => clientAxios.get(`/rooms/${roomId}/details`), // Example: for client to see their room info
};